package master;

/**
 * 搜索旋转排序数组2
 *
 * @author chenyw
 * @date 2022/7/30 16:47
 */
public class M81search {
    public boolean search(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left <= right) {
            int mid = (left + right) / 2;
            //处理重复元素
            while (left < right && nums[left] == nums[left +1]) left++;
            while (left < right && nums[right] == nums[right - 1]) right--;
            if (nums[mid] == target) {
                return true;
            } else if (nums[left] <= nums[mid]) {
                if (nums[left] <= target && target < nums[mid]) {
                    right = mid - 1;
                } else {
                    left = mid + 1;
                }
            } else {
                if (nums[mid] < target && target <= nums[right]) {
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
        }
        return false;
    }
}
